import { fetchData } from "../utils/axios-test";
import {ref,Ref} from 'vue'
import request from "../utils/request";
import { IWorkbookData } from "@univerjs/core";
import { DEFAULT_EXCEL_MARK } from "../assets/default-workbook-data";

function useExcelLoad(univerRef:Ref<any>,data:Ref<IWorkbookData>){
    const testValue:Ref<string>=ref('');

    // 测试
    function fetchTest(){
        return request.get('/onlineExcel/fetchTest').then((res:any)=>{
            testValue.value=res.data;
            return res;
        })
    }

    const mark=ref(DEFAULT_EXCEL_MARK);

    // 读取数据
    async function loadData(){
        const remoteResp=await request.get('/onlineExcel/load/'+mark.value)
        const resultData=remoteResp.data.data
        if(resultData){
            console.log('hook load:',resultData)
            univerRef.value.setData(resultData)
            data.value=resultData
            // univerRef.value.reset()

            sessionStorage.setItem('excel_mark',mark.value)
            sessionStorage.setItem('excel_content',JSON.stringify(resultData))

            location.reload()
        }
    }

    // 保存数据
    async function saveData(){
        const data=univerRef.value?.getData()
        return request.post('/onlineExcel/save/'+mark.value,data).then((res:any)=>{
            console.log('hook save result:',res);
            return res;
        })
    }

    return {
        mark,testValue,fetchTest,saveData,loadData
    }
}

export {useExcelLoad}